package com.example.springboot.controller;


import com.example.springboot.Service.ScheduleSave;
import com.example.springboot.entity.Schedule;
import com.example.springboot.entity.Student;
import com.example.springboot.mapper.DepartmentMapper;
import com.example.springboot.mapper.SperMapper;
import com.example.springboot.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/user")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private SperMapper sperMapper ;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ScheduleSave scheduleSave;

//  管理员学生信息接口
    @GetMapping("/student")
    public Map<String,Object>  index(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        pageNum = (pageNum - 1)*pageSize;
        Map<String,Object> res = new HashMap<>();
        List<Student> students = studentMapper.findAll(pageSize,pageNum);
        students.forEach(student -> {
            student.setDepartment_name(departmentMapper.findDepartement_name(sperMapper.findDepartement_id(student.getSper_code())));
            student.setSper_name(sperMapper.findSperName(student.getSper_code()));
        });
//        students.forEach(student ->);
        res.put("data",students);
        res.put("Total",studentMapper.findCount());
        res.put("zhuanye",sperMapper.sperName());
        return res;
    }

    @GetMapping("/login")
    public String login(@RequestParam Integer userAccount, @RequestParam String userPassword,@RequestParam String userClass){
        //   账号和表名查询密码
        String pass = "student_id";
        switch (userClass){
            case "student" : pass =studentMapper.authenticate_student(userAccount) ;break;
            case "teacher" : pass = studentMapper.authenticate_teacher(userAccount);break;
            case "admin" : pass = studentMapper.authenticate_admin(userAccount);break;
        }
        if (Objects.equals(pass, userPassword)){
            return "1";
        }else
            return "0";
    }  //  1 代表成功 0 代表失败

//    得到学生需要课程的课程信息
    @GetMapping("/stuClass")
    public List<Schedule> schedule(@RequestParam Integer student_id){
        return scheduleSave.ScheduleSave(student_id);
    }


//    得到学生的个人中心
    @GetMapping("/person")
    public Student studentPerson(@RequestParam Integer student_id){
        Student student =  studentMapper.findStudent_msg(student_id);
        student.setSper_name(sperMapper.findSperName(student.getSper_code()));
        student.setDepartment_name(departmentMapper.findDepartement_name(sperMapper.findDepartement_id(student.getSper_code())));
        return student;
    }
    @PostMapping ("setStudent")
    public int setStudent(@RequestBody Student student){
           return studentMapper.update_student(student);
    }


    @GetMapping("likeCourse")
    public List<Student> likeStudent(@RequestParam String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        if (pattern.matcher(str).matches()){
//              是数字
            return studentMapper.student_idSelect(str);
        }else {
            return studentMapper.student_name_like("%"+str+"%");
        }

    }
    @GetMapping("getStudentSper")
    public List<Student> getStudentSper(@RequestParam String sper_name){
        return studentMapper.getStudentSper(sper_name);

    }

}
